#!coding=utf-8
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.http.response import HttpResponse
from django.core.exceptions import ObjectDoesNotExist
import json
from shop.models import CustAdr, Goods, Order, OrderItem,DeductionItem,UserFavorite

def suggest(request):
    template='suggest.html'
    return render_to_response(template,context_instance=RequestContext(request))

def deladdress(request):
    idno=request.POST.get('id')
    try:
        tmpadr=CustAdr.objects.get(id=int(idno));  # @UndefinedVariable
        tmpadr.delete();
    except ObjectDoesNotExist:
        return HttpResponse("error")
    return HttpResponse("true")

def deluserfavorite(request):
    idno=request.POST.get('id')
    try:
        tmpfav=UserFavorite.objects.get(id=int(idno));  # @UndefinedVariable
        tmpfav.delete();
    except ObjectDoesNotExist:
        return HttpResponse("error")
    return HttpResponse("true")

def backmima(request):
	template='backmima.html'
	return render_to_response(template,context_instance=RequestContext(request))
def denglu(request):
	template='denglu.html'
	return render_to_response(template,context_instance=RequestContext(request))
def dingdanchenggong(request):
	template='dingdanchenggong.html'
	return render_to_response(template,context_instance=RequestContext(request))
def gerenziliao(request):
	template='gerenziliao.html'
	return render_to_response(template,context_instance=RequestContext(request))
def gerenziliaobianji(request):
	template='gerenziliaobianji.html'
	return render_to_response(template,context_instance=RequestContext(request))
def huiyuanshouye(request):
	template='huiyuanshouye.html'
	return render_to_response(template,context_instance=RequestContext(request))
def index(request):
	template='index.html'
	return render_to_response(template,context_instance=RequestContext(request))
def pinglun(request):
	template='pinglun.html'
	return render_to_response(template,context_instance=RequestContext(request))
def quehuo(request):
	template='quehuo.html'
	return render_to_response(template,context_instance=RequestContext(request))
def queren_nodizhi(request):
	template='queren_nodizhi.html'
	return render_to_response(template,context_instance=RequestContext(request))
def queren_youdizhi(request):
	template='queren_youdizhi.html'
	return render_to_response(template,context_instance=RequestContext(request))
def shangpinlist(request):
	template='shangpinlist.html'
	return render_to_response(template,context_instance=RequestContext(request))
def shoppinglist(request):
	template='shoppinglist.html'
	return render_to_response(template,context_instance=RequestContext(request))
def shouhuodizhi(request):
	template='shouhuodizhi.html'
	return render_to_response(template,context_instance=RequestContext(request))
def sousuo(request):
	template='sousuo.html'
	return render_to_response(template,context_instance=RequestContext(request))
def sousuojieguo(request):
	template='sousuojieguo.html'
	return render_to_response(template,context_instance=RequestContext(request))
def wodedingdan(request):
	template='wodedingdan.html'
	return render_to_response(template,context_instance=RequestContext(request))
def xiangqing(request):
	template='xiangqing.html'
	return render_to_response(template,context_instance=RequestContext(request))
def xinzengshouhuo(request):
	template='xinzengshouhuo.html'
	return render_to_response(template,context_instance=RequestContext(request))
def xiugaimima(request):
	template='xiugaimima.html'
	return render_to_response(template,context_instance=RequestContext(request))
def yueshenghuo(request):
	template='yueshenghuo.html'
	return render_to_response(template,context_instance=RequestContext(request))
def zhanghaoshezhi(request):
	template='zhanghaoshezhi.html'
	return render_to_response(template,context_instance=RequestContext(request))
def zhuce(request):
	template='zhuce.html'
	return render_to_response(template,context_instance=RequestContext(request))
def list_by_caga(request):
	template='list_by_caga.html'
	return render_to_response(template,context_instance=RequestContext(request))
	
def dingdanxiangqing(request):
	template='dingdanxiangqing.html'
	return render_to_response(template,context_instance=RequestContext(request))
def pingjia(request):
	template='pingjia.html'
	return render_to_response(template,context_instance=RequestContext(request))
	
def xianjinzhanghu(request):
	template='xianjinzhanghu.html'
	return render_to_response(template,context_instance=RequestContext(request))
def wodeyouhuijuan(request):
	template='wodeyouhuijuan.html'
	return render_to_response(template,context_instance=RequestContext(request))

def kefulogin(request):
	template='kefulogin.html'
	return render_to_response(template,context_instance=RequestContext(request))
	
def kefuhome(request):
	template='kefuhome.html'
	return render_to_response(template,context_instance=RequestContext(request))
	
def chatwithkefu(request):
	template='chatwithkefu.html'
	return render_to_response(template,context_instance=RequestContext(request))
def index_pc(request):
	template='index_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))

def introduction_pc(request):
	template='introduction_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))
	
def Listshangping_pc(request):
	template='Listshangping_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))
	
	
def shopcart_pc(request):
	template='shopcart_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))
	
def address_pc(request):
	template='address_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))

def bill_pc(request):
	template='bill_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))

def billlist_pc(request):
	template='billlist_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def bonus_pc(request):
	template='bonus_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	
def collection_pc(request):
	template='collection_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def commentlist_pc(request):
	template='commentlist_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def coupon_pc(request):
	template='coupon_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def GeRen_pc(request):
	template='GeRen_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def information_pc(request):
	template='information_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def login_pc(request):
	template='login_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def order_pc(request):
	template='order_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def orderinfo_pc(request):
	template='orderinfo_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def password_pc(request):
	template='password_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def pay_pc(request):
	template='pay_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def refund_pc(request):
	template='refund_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def register_pc(request):
	template='register_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

	
def safety_pc(request):
	template='safety_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def search_pc(request):
	template='search_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

	
def setpay_pc(request):
	template='setpay_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	


def success_pc(request):
	template='success_pc.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def erweimalogin(request):
	template='erweimalogin.html'
	return render_to_response(template,context_instance=RequestContext(request))	
	
def erweimaweixinpay(request):
	template='erweimaweixinpay.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def myshoucang(request):
	template='myshoucang.html'
	return render_to_response(template,context_instance=RequestContext(request))	

def addorder(request):
    order_openid=""
    order_custname=""
    order_phoneno=""
    order_cityname=""
    order_detailAdrinfo=""
    order_beizhu=""
    order_price=0
    order_count=0
    jy_price=0
    act_price=0
    deductionid=""
    received_json_data = json.loads(request.body)
    order_openid=received_json_data["openid"];
    order_beizhu=received_json_data["beizhu"];
    custadrid=received_json_data["custadr"];
    deductionItemid=received_json_data["deductionitem"];
    if custadrid:
        try:
            print custadrid
            tmpcustadr=CustAdr.objects.get(id=int(custadrid))
            order_custname=tmpcustadr.custname;
            order_phoneno=tmpcustadr.phoneno;
            order_cityname=tmpcustadr.cityname;
            order_detailAdrinfo=tmpcustadr.detailAdrinfo;
        except ObjectDoesNotExist:
            res={"code":"error","error":"收货地址异常！"}
            encodedjson = json.dumps(res)
            return HttpResponse(encodedjson)
        
    for i in range(len(received_json_data["detail"])):
        jy_price+=float(received_json_data["detail"][i]["price"]);
        tmpcount=int(received_json_data["detail"][i]["count"]);
        order_count+=tmpcount
        good_id=received_json_data["detail"][i]["id"]
        try:
            good=Goods.objects.get(id=int(good_id))
            tmp_price=good.price;
            order_price+=tmp_price*tmpcount;
            tmpsale=good.sales;
            good.sales=tmpsale+tmpcount
            good.save()
        except ObjectDoesNotExist:
            res={"code":"error","error":"商品信息异常！"}
            encodedjson = json.dumps(res)
            return HttpResponse(encodedjson)
    if deductionItemid != 0:
        tmpdedu=DeductionItem.objects.get(id=int(deductionItemid))
        if int(order_price*100) > int(tmpdedu.deduction.condi_value*100) :
            tmpdedu.status="已使用";
            act_price=float(order_price)-float(tmpdedu.deduction.deduct_value)
            tmpdedu.save();
        else:
            res={"code":"error","error":"不满足满减条件"}
            encodedjson = json.dumps(res)
            return HttpResponse(encodedjson)
    else:
        act_price=float(order_price)
    if abs(float(order_price)-float(jy_price)) < 0.00001:
#         float的值比较不可以用==
#     if (order_price == jy_price):
        #录入订单数据
        tmporder=Order(open_id=order_openid,customer_remark=order_beizhu,custcont=order_beizhu,price=order_price,count=order_count, \
              custname=order_custname,act_price=act_price,phoneno=order_phoneno,cityname=order_cityname,detailAdrinfo=order_detailAdrinfo,deductionitemid=str(deductionItemid));
        tmporder.save();
        #录入订单详情数据
        try:
            #如果一个用户有多条相同的订单，取最大id为最近订单
            getorder=Order.objects.filter(open_id=order_openid,customer_remark=order_beizhu,custcont=order_beizhu,price=order_price,count=order_count, \
                  custname=order_custname,phoneno=order_phoneno,cityname=order_cityname,detailAdrinfo=order_detailAdrinfo).order_by('-id');
            
            for i in range(len(received_json_data["detail"])):
                orderitem_goodid=int(received_json_data["detail"][i]["id"])
                orderitem_good=Goods.objects.get(id=int(orderitem_goodid))
                orderitem_count=int(received_json_data["detail"][i]["count"])
                ordertem_good_format=received_json_data["detail"][i]["xiangqing"]
                ordertem_price=float(received_json_data["detail"][i]["price"])
                tmporderitem=OrderItem(good=orderitem_good,count=orderitem_count,order=getorder[0],price=ordertem_price,good_format=ordertem_good_format)
                tmporderitem.save();
            
            res={"orderid":getorder[0].id}
            encodedjson = json.dumps(res)
            return HttpResponse(encodedjson)
        
        except ObjectDoesNotExist:
            res={"code":"error","error":"订单录入有误！"}
            encodedjson = json.dumps(res)
            return HttpResponse(encodedjson)
    else:
        res={"code":"error","error":"订单价格校验不对称！"}
        encodedjson = json.dumps(res)
        return HttpResponse(encodedjson)  
